package com.anlogic.sdk.bootimage.internal.ui.composite;

import com.anlogic.sdk.bootimage.internal.ui.AutoCompleteProvider;
import com.anlogic.sdk.bootimage.internal.ui.common.IUIValidator;
import com.anlogic.sdk.bootimage.model.BootGenModel;
import com.anlogic.sdk.utils.StringUtils;
import com.anlogic.sdk.utils.UIUtils;
import com.anlogic.sdk.utils.options.ChipType;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

public abstract class AbsBootgenComposite extends Composite implements IBootgenUIContants {

	protected AutoCompleteProvider.IAutoCompeletHelper autoCompleteHelper;

	protected BootGenModel model;

	private boolean skipNotifactions;

	protected List<AutoCompleteProvider> bifPathDependentProviders = new ArrayList<>();

	private List<Control> archDependndentControls = new ArrayList<>();

	private IUIValidator uiValidator;

	public AbsBootgenComposite(Composite parent, int style, BootGenModel model,
			AutoCompleteProvider.IAutoCompeletHelper autoCompeletemHelper) {
		super(parent, style);
		this.model = model;
		this.autoCompleteHelper = autoCompeletemHelper;
	}

	public AbsBootgenComposite(Composite parent, int style, BootGenModel model) {
		this(parent, style, model, null);
	}

	protected void bindToArchitecture(Control control, ChipType chipType) {
		control.setData("archtype", chipType);
		this.archDependndentControls.add(control);
	}

	protected void addAutoCompletionCapability(Text text) {
		this.bifPathDependentProviders.add(new AutoCompleteProvider(text, getAutoCompleteHelper()));
	}

	protected AutoCompleteProvider.IAutoCompeletHelper getAutoCompleteHelper() {
		if (this.autoCompleteHelper == null)
			this.autoCompleteHelper = new AutoCompleteProvider.IAutoCompeletHelper() {
				public String getRelativePath() {
					if (StringUtils.isEmpty(AbsBootgenComposite.this.model.getBifFile()))
						return null;
					return (new File(AbsBootgenComposite.this.model.getBifFile())).getParent();
				}
			};
		return this.autoCompleteHelper;
	}

	public void setSkipNotifactions(boolean skipNotifactions) {
		this.skipNotifactions = skipNotifactions;
	}

	public boolean isSkipNotifactions() {
		return this.skipNotifactions;
	}

	public void updateModel(BootGenModel model) {
		this.model = model;
		refreshUI();
	}

	public void refreshUI() {
		refreshContentProposals();
		updateArchBasedControls(this.model);
		setSkipNotifactions(true);
		loadUI();
		setSkipNotifactions(false);
	}

	public void refreshAndValidateUI() {
		refreshUI();
		validateUI();
	}

	protected void markEditedAndValidate() {
		setModelEdited();
		validateUI();
	}

	public void validateUI() {
		if (this.uiValidator != null)
			this.uiValidator.validate();
	}

	public void setUiValidator(IUIValidator uiValidator) {
		this.uiValidator = uiValidator;
	}

	public void refreshContentProposals() {
		for (AutoCompleteProvider provider : this.bifPathDependentProviders)
			provider.refreshProposals();
	}

	public void setModelEdited() {
		if (!this.model.isNew())
			this.model.setEditBif(true);
	}

	private void updateArchBasedControls(BootGenModel model) {
		for (Control control : this.archDependndentControls) {
			ChipType ctrlChipType = (ChipType) control.getData("archtype");
			ChipType modelChipType = model.getChipType();
			boolean exclude = (ctrlChipType != modelChipType);
			UIUtils.excludeWidget(control, exclude);
			control.getParent().layout(true);
		}
	}

	public void okPressed() {
	}

	public abstract void addListeners();

	public abstract void loadUI();
}
